package com.example.atiplat_vodcp;

import java.io.File;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Random;

import demo.playfile.service.AtiHttpServer;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	
	WebView webView;
	 public static String getTrace(Throwable t) {
		 try {
			 StringWriter stringWriter= new StringWriter();
		        PrintWriter writer= new PrintWriter(stringWriter);
		        t.printStackTrace(writer);
		        StringBuffer buffer= stringWriter.getBuffer();
		        return buffer.toString();
		} catch (Exception e) {
			return "$getTrace_ex";
		}
	       
	    }
	@SuppressLint({ "SetJavaScriptEnabled", "JavascriptInterface" })
	public void form_load() {
		try {
			
			jettyStart();
		String simple = "tipsss...1";
				//PinyinX.getSimple(  "Android ��ʾ");
//		new AlertDialog.Builder(this).setTitle(simple)
//					.setMessage("����һ����ʾ,��ȷ��").show();
			webView = (WebView) findViewById(R.id.webView1);
			webView.setWebViewClient(new WebViewClient());
		//	WebSettings webSettings = webView.getSettings();
			// ����WebView���ԣ��ܹ�ִ��Javascript�ű�
			webView.getSettings().setJavaScriptEnabled(true);
			webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		//	setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口 
			webView.getSettings().setAllowContentAccess(true);
			webView.getSettings().setAllowFileAccess(true);
			webView.getSettings().setLoadsImagesAutomatically(true);
			String url = "http://www.baidu.com";
			url="file:///android_asset/cms/list.html";
			url="http://www.zhida84.com/cmsPhone4jobusImp/list.html";
			url="http://192.168.2.114:8080/cms/list_detail.html?a="+new Random().nextInt();
		//	url="http://192.168.2.114:8080/cms/t.html?a="+new Random().nextInt();
			 
			   webView.addJavascriptInterface(new JsBridge(this), "jsBridge");
			   webView.addJavascriptInterface(new browExtObj(this), "browExt");
			   webView.addJavascriptInterface(new JsBridge(this), "AtiJsBridge");
		 	webView.loadUrl(url);
		 	
			 Log.v(":::::::::::tag11","startURI:::::::::" ); 
			
		//	MediaPlayer
		//	play2();

		} catch (Throwable e) {
			Log.i("exp",  getTrace(e));
			String trace = getTrace(e);
		 
			Toast.makeText(mainActivity,trace,					Toast.LENGTH_LONG).show();
		}
	}
	MainActivity mainActivity;

	private void jettyStart() {
		mainActivity = this;
		Runnable r = new Runnable() {
			public void run() {
				try {
					//jetty6FilesServer.main(null);
					AtiHttpServer.main(null);
					Log.i("::::jetty", "jettyStart  ok");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					String trace = getTrace(e);
					Log.i("::::jettyStart  exp", trace);
					Looper.prepare();
					Toast.makeText(mainActivity, trace, Toast.LENGTH_LONG)
							.show();
					Looper.loop();
				}

			}
		};
		new Thread(r).start();

	}
	private void play2() {
		//String mv="\\\\192.168.2.106\\e\\������\\9·ڤ��\\9·ڤ��.mkv";
		String mv = "smb://192.168.2.106/e/������/9·ڤ��/9·ڤ��.mkv";
		Toast.makeText(this, "player in main ,mv:"+new File(mv).exists(), Toast.LENGTH_LONG).show();
String mv2=	Environment.getExternalStorageDirectory().getPath()+"/Test_Movie.m4v";
		Uri uri = Uri.parse(mv);  
		//����ϵͳ�Դ��Ĳ����� 
		    Intent intent = new Intent(Intent.ACTION_VIEW); 
		    Log.v("URI:::::::::", uri.toString()); 
		 //   intent.setData(uri); 
		    intent.setDataAndType(uri, "video/*");
		    startActivity(intent);
		System.out.println("--form_load finish");
	}
	
	@Override
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	  
	  if (keyCode == KeyEvent.KEYCODE_MENU) { 
	   // 在这里做你想做的事情
	  // super.openOptionsMenu();  // 调用这个，就可以弹出菜单
			webView.loadUrl("javascript:KEYCODE_MENU_handle()");  
		  
	  } 
	  
	  if (keyCode == KeyEvent.KEYCODE_BACK) { 
		   // 在这里做你想做的事情
		  // super.openOptionsMenu();  // 调用这个，就可以弹出菜单
				webView.loadUrl("javascript:KEYCODE_BACK_handle()");  
			  
		  }   
	  return true; // 最后，一定要做完以后返回 true，或者在弹出菜单后返回true，其他键返回super，让其他键默认
	 }


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		try{
			this.requestWindowFeature(Window.FEATURE_NO_TITLE);//ȥ��������  
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		 form_load();
		}catch(Exception e)
		{
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
			.show();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
